Passed
Push — main ( ddcd3f...98b49f )
by Andrii
02:26
created

pipe.ts ➔ exclusion   A

Complexity

Conditions 3

Size

Total Lines 26
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 3
eloc 22
dl 0
loc 26
rs 9.352
c 0
b 0
f 0
1
import { ClassValue } from "../../defs"
2
3
export {}
4
5
// Nope
6
// type tExcluder<S extends Record<string, ClassValue>>
7
// = (
8
//   <E extends{[K in keyof S]?: ClassValue}>(exclude: E) => typeof exclude extends Record<infer E, ClassValue>
9
//   ? { [P in Exclude<keyof S, keyof E>]: ClassValue; } & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassValue; }>
10
//   : never
11
// );
12
13
14
// type tExcluder<S extends Record<string, ClassValue>>
15
// = (
16
//   <E extends Record<string, ClassValue>>(exclude: E) => { [P in Exclude<keyof S, keyof E>]: ClassValue; }
17
//   & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassValue; }>
18
// );
19
20
type tExcluder<S extends Record<string, ClassValue>>
21
= (
22
  <E extends Record<string, ClassValue>>(exclude: E) => keyof E extends keyof S
23
  ? { [P in Exclude<keyof S, keyof E>]: ClassValue; }
24
    & tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassValue; }>
25
  : {[P in Exclude<keyof E, keyof S>]: never;}
26
);
27
28
function exclusion<
29
  S extends Record<string, ClassValue>,
30
  E extends {[K in keyof S]?: ClassValue}
31
>(
32
  source: S, ex: E
33
) {
34
35
  const filtered = {...source}
36
  for (const k in ex) {
37
    delete filtered[k]
38
  }
39
  
40
  const host = (
41
    e: { [P in Exclude<keyof S, keyof E>]?: ClassValue; }
42
  ) => exclusion(
43
    filtered as { [P in Exclude<keyof S, keyof E>]: ClassValue; },
44
    e
45
  )
46
47
  for (const key in filtered)
48
    //@ts-expect-error
49
    host[key]
50
    = filtered[key]
51
52
  return host as tExcluder<{ [P in Exclude<keyof S, keyof E>]: ClassValue; }>
53
}
54
55
const source: Record<"a"|"b"|"c"|"d"|"e", ClassValue> = {a: "a", b: undefined, c: "c", d: undefined, e: undefined}
56
57
const step0 = exclusion(
58
  source,
59
  //@ts-expect-error 'z' does not exist in type
60
  {z: undefined}
61
)
62
, answ0: typeof step0 = {
63
  //@ts-expect-error
64
  whatever: true
65
}
66
, step1 = exclusion(source, {a: "a", b: undefined})
67
68
//@ts-expect-error
69
step1({"c": undefined, "z": undefined})
70
()
71
72
const step2 = step1({"c": undefined})
73
//@ts-expect-error
74
, {c} = step2
75
, step3 = {...step2({
76
  "z": ""
77
})}
78
, key: keyof typeof step3 = "z"
79
//@ts-expect-error Property 'd' is missing
80
, answ
81
: typeof step2 = {
82
  e: "",
83
  //@ts-expect-error Object literal may only specify known properties, and 'z'
84
  z: ""
85
}
86
export {step2, answ0, key}